package userfollow

import (
	"context"
	"time"

	"mall/common/globalkey"
	"mall/common/resultx"
	"mall/service/user/model"
	"mall/service/user/rpc/internal/svc"
	"mall/service/user/rpc/types/user"

	"github.com/volatiletech/null/v8"
	"github.com/zeromicro/go-zero/core/logx"
)

type CreateUserFollowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateUserFollowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserFollowLogic {
	return &CreateUserFollowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// #25.01.06#  用户关注
func (l *CreateUserFollowLogic) CreateUserFollow(in *user.UserFollowInfo) (*user.BaseResp, error) {
	if in.UserId == nil || *in.UserId == 0 {
		return nil, resultx.StatusError(resultx.USER_FOLLOW_USER_ID_REQUIRED, "")
	}
	if in.FriendId == nil || *in.FriendId == 0 {
		return nil, resultx.StatusError(resultx.USER_FOLLOW_FRIEND_ID_REQUIRED, "")
	}
	if in.FollowChannel == nil || *in.FollowChannel == 0 {
		return nil, resultx.StatusError(resultx.USER_FOLLOW_FOLLOW_CHANNEL_REQUIRED, "")
	}
	if in.Status == nil || *in.Status == 0 {
		return nil, resultx.StatusError(resultx.USER_FOLLOW_STATUS_REQUIRED, "")
	}
	//是否被对方拉黑
	blackFilterMap := make(map[string]map[string][]any)
	blackFilterMap[model.UserFollowRelationshipColumns.UserID] = map[string][]any{
		"equalTo": {*in.FriendId},
	}
	blackFilterMap[model.UserFollowRelationshipColumns.FriendID] = map[string][]any{
		"equalTo": {*in.FriendId},
	}
	blackFilterMap[model.UserFollowRelationshipColumns.Status] = map[string][]any{
		"equalTo": {globalkey.UserFollowStatusDeleted},
	}
	blackList, _, err := l.svcCtx.UserFollow.GetList(l.ctx, model.M{}, -1, blackFilterMap)
	if err != nil {
		return nil, err
	}
	if len(blackList) > 0 {
		return nil, nil
	}
	filterMap := make(map[string]map[string][]any)
	filterMap[model.UserFollowRelationshipColumns.UserID] = map[string][]any{
		"equalTo": {*in.UserId},
	}
	filterMap[model.UserFollowRelationshipColumns.FriendID] = map[string][]any{
		"equalTo": {*in.FriendId},
	}
	list, _, err := l.svcCtx.UserFollow.GetList(l.ctx, model.M{}, -1, filterMap)
	if err != nil {
		return nil, err
	}
	now := time.Now()
	if len(list) > 0 {
		userFollow := list[0]
		userFollow.FollowChannel = uint16(*in.FollowChannel)
		userFollow.Status = uint16(*in.Status)
		if *in.Status == globalkey.UserFollowStatusEnabled {
			userFollow.LastCreatedAt = null.TimeFrom(now)
		}
		userFollow.UpdatedAt = now
		err := l.svcCtx.UserFollow.Update(l.ctx, nil, userFollow, nil)
		if err != nil {
			return nil, err
		}
		return &user.BaseResp{
			Msg: "success",
		}, nil
	}

	err = l.svcCtx.UserFollow.Insert(l.ctx, nil, &model.UserFollowRelationship{
		UserID:        *in.UserId,
		FriendID:      *in.FriendId,
		FollowChannel: uint16(*in.FollowChannel),
		Status:        uint16(*in.Status),
		LastCreatedAt: func() null.Time {
			if *in.Status != globalkey.UserFollowStatusEnabled {
				return null.TimeFrom(time.Time{})
			}
			return null.TimeFrom(now)
		}(),
		CreatedAt: now,
		UpdatedAt: now,
	})
	if err != nil {
		return nil, err
	}
	return &user.BaseResp{
		Msg: "success",
	}, nil
}
